package com.njlime.member.repository.app;

import com.njlime.cms.sdk.weibo4j.model.User;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.DateUtil;
import com.njlime.member.entiies.MemberFocus;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017/10/9.
 */
@Repository
public class AppMemberFocusDaoImpl extends BaseDaoImpl<MemberFocus> {

    /**
     * 获取粉丝关注
     * @param pageBean
     * @param member_id
     * @return
     */
    public PageBean getByMemberId(PageBean pageBean, Integer member_id) {
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select u.* from t_member_focus t ");
        sb.append("join t_unit u on u.id = t.unit_id ");
        sb.append("where t.member_id = ? ");
        arrList.add(member_id);
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by t.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 查询是否关注
     * @param unit_id
     * @param user_id
     * @return
     */
    public int isFocus(int unit_id, int user_id){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_member_focus where unit_id = ? and member_id = ? ");
        arrList.add(unit_id);
        arrList.add(user_id);
        return getListJdbcSize(sb.toString(),arrList);
    }

    /**
     * 删除
     * @param unit_id
     * @param user_id
     */
    public void del(int unit_id, int user_id){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from t_member_focus where unit_id = ? and member_id = ? ");
        arrList.add(unit_id);
        arrList.add(user_id);
        updateJdbc(sb.toString(),arrList);
    }

    /**
     * 粉丝数
     * @param unit_id
     * @return
     */
    public int focusNum(int unit_id){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select IFNULL(count(1),0) from t_member_focus where unit_id = ? ");
        arrList.add(unit_id);
        return Integer.valueOf(findCount(sb.toString(),arrList).toString());
    }
}
